package com.hbl.huangbaolian1506101164.Activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hbl.huangbaolian1506101164.R;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import layout.FondFragment;
import layout.HomeFragment;
import layout.SignFragment;
import layout.UserFragment;


public class Home extends FragmentActivity {


    @InjectView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @InjectView(R.id.txt_H)
    TextView txtH;
    @InjectView(R.id.txt_F)
    TextView txtF;
    @InjectView(R.id.add)
    ImageView add;
    @InjectView(R.id.txt_S)
    TextView txtS;
    @InjectView(R.id.txt_U)
    TextView txtU;
    @InjectView(R.id.tab_menu)
    LinearLayout tabMenu;
    @InjectView(R.id.div_tab_bar)
    View divTabBar;
    @InjectView(R.id.activity_home)
    RelativeLayout activityHome;
    //Fragment管理器
    private FragmentManager fm = this.getSupportFragmentManager();
    private FragmentTransaction ft;
    private HomeFragment Home;
    private FondFragment Fond;
    private SignFragment Sign;
    private UserFragment User;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.inject(this);
        replaceFragment(new HomeFragment());
        initView();
        //开始事务（每次改变Fragment管理器之后都要提交）
        ft = fm.beginTransaction();
        //提交事务
        ft.commit();
    }
    /**
     * 替换帧布局里面的内容
     * @param fragment
     */
    private void replaceFragment(android.support.v4.app.Fragment fragment){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.commit();
    }
    private void initView() {
        txtH.setSelected(true);
    }

    @OnClick({R.id.txt_H, R.id.txt_F, R.id.add, R.id.txt_S, R.id.txt_U})
    public void onViewClicked(View view) {
        //每次点击时都需要重新开始事务
        ft = fm.beginTransaction();
        //把显示的Fragment隐藏
        setSelected();
        switch (view.getId()) {
            case R.id.txt_H:
                txtH.setSelected(true);
                if (Home == null) {
                    Home = new HomeFragment();
                    ft.add(R.id.fragment_container, Home);
                } else {
                    ft.show(Home);
                }
                break;
            case R.id.txt_F:
                txtF.setSelected(true);
                if (Fond == null) {
                    Fond = new FondFragment();
                    ft.add(R.id.fragment_container, Fond);
                } else {
                    ft.show(Fond);
                }
                break;
            case R.id.add:
                break;
            case R.id.txt_S:
                txtS.setSelected(true);
                if (Sign == null) {
                    Sign = new SignFragment();
                    ft.add(R.id.fragment_container, Sign);
                } else {
                    ft.show(Sign);
                }
                break;
            case R.id.txt_U:
                txtU.setSelected(true);
                if (User == null) {
                    User = new UserFragment();
                    ft.add(R.id.fragment_container, User);
                } else {
                    ft.show(User);
                }
                break;
        }
        ft.commit();
    }
    private void setSelected(){
        txtH.setSelected(false);
        txtF.setSelected(false);
        txtS.setSelected(false);
        txtU.setSelected(false);
        if(Home != null){
            //隐藏Fragment
            ft.hide(Home);
        }
        if(Fond != null){
            ft.hide(Fond);
        }
        if(Sign != null){
            ft.hide(Sign);
        }
        if(User != null){
            ft.hide(User);
        }
    }
}
